Dockerize your First .NET Core application

Recently I tried to containerize one of my microservices using Docker. While doing that I learned a few details on the docker which I have tried to capture in this post.

In this article, we shall cover below aspects,

“Docker is an open platform for developing, shipping and running applications. Docker enables you to separate your applications from your infrastructure so you can deliver software quickly.” _Docker

The main advantage of the containerized app is that the image created through docker containerization can be hosted anywhere, be it a private cloud or public cloud (ex. Azure or AWS).

Getting started

Prerequisites

  • .NET Core 3.1 or .NET 6
  • Docker installation – Please see here for troubleshooting steps details if any while installing Docker.

Create an ASP.NET Core API Application

Here I am using Visual Studio to create ASP.NET Core service.

You can use ASP.NET Core 3.1 ot .NET 6 project template as required.

I have used the default API template and here is the output of GET API,

Let’s now publish the application as shown below. All the binaries once published successfully will be located in the working directory “docker-container-service\bin\Release\netcoreapp2.2\publish

blank

Create a Docker File

Once you have all your binaries published to the ‘publish’ folder, here onward we will be using docker specific commands to containerize our first micro-service.

To simplify the process go to the publish folder and open any CLI tool. Create new docker file using below command,

echo . > Dockerfile

blank

Add Metadata in the Docker File

Open the docker file and add below metadata to a file,

Dockerfile:


FROM microsoft/dotnet:2.2-aspnetcore-runtime
COPY / app/                                                        
ENTRYPOINT ["dotnet", "app/docker-container-service.dll"]                                                                       

Above command is explained as below,

FROM
  • This command pulls down the image tagged from the microsoft/dotnet:3.1-aspnetcore-runtime repository.
COPY

This command copy the current content from publish folder to a folder ‘app’ in the container.

ENTRYPOINT

This command configures the container to run using the given entry point.

Create and Build the Docker Image

From the command prompt please run the below command,

CLI:

docker build -t mydockerservice .

Above command is explained as below,

docker build

This command uses the information from local Dockerfile to build a Docker image.


-t mydockerservice

This command names the new created image as specified. Here we are providing the name as ‘mydockerservice’

.

Here dot specifies which directory to find by Dockerfile. In our case, it is the current directory. So all binaries from the ‘publish’ folder will be considered for imaging.

Here you go with actual command execution,

blank

Run Docker Image

Run docker images locally to verify if everything is working fine

docker run -it –rm -p 3000:80 mydockerservice

The above command is explained as below,

docker run

This command creates and starts the container, so performs two operations using a single command. Overall command ‘docker run’ wraps below two commands,

  • docker create
  • docker start

-it --rm

This command connects the current terminal to the container. ‘–rm‘ command checks the container if it is automatically deleted when the process is stopped.

After running the images locally we will invoke one of our REST API.

As shown below our container is ready and hosted on port 3000:80.

blank

After executing the GET API through browser, here is the result,

blank

If you wish to see details on current available docker images on your machine, please use below command,

blank

Finally, first containerized .NET Core micros-service ready and steady for deployment. Now this container can be deployed to Server or Public or Private Cloud of your choice.

Publishing Image to Docker Hub

So far we verified the docker image locally. Now let’s publish the image to Docker Hub so that it becomes consumable as private or publically accessible. Publishing to the docker hub consists of 2 steps as below,

  • Build the image against your docker account and
  • Pushing the image to docker.

Login to docker with your docker id and credentials,

blank

Build the image

Build your docker images using the below command,

docker image build -t --rm firstthecodebuzz/microservice1:1.0 .

Here ‘firstthecodebuzz ‘ is the docker account name. Please your account name here. Command details remains same as I already explained above.

Dockerize First NET Core

Push the image

docker push firstthecodebuzz/microservice1

blank

Run the image from Docker Hub

Let’s run the images directly from the docker.

blank

This image is now available to consume. I can now perform all my services operation.

Let’s invoke GET API and get the result from docker.

blank

Summary

Docker platform allows us to develop, deploy, and run applications with containers. In this article, we learned to containerize our .NET Core microservice using Docker. Docker is a huge subject and here I tried to highlight very basic functionalities of docker containerization. Hope you liked this article.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



4 thoughts on “Dockerize-Containerize your First .NET Core application

  1. While i following this , i got few issue, due to some reason my docker on my machine is not recognizing the commands and keep throwing the error.

  2. Very informative!. Thanks. Future is going to be of Containerization helping both business and developers.

Leave a Reply

Your email address will not be published. Required fields are marked *